#include "database_driver.h"

Database_driver::Database_driver()
{

}

//通过connectionname设置数据库
void Database_driver::setConnectByName(QString database_path , QString connectionname)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE" , connectionname);
    db.setDatabaseName(database_path);
    if(db.open()){
        qDebug()<<"打开数据库成功！";
    }else{
        qDebug()<<"打开数据库失败";
        qDebug()<<db.lastError();
    }
}

//通过connectionname查找数据库
QSqlDatabase Database_driver::getConnectByName(QString connectionname)
{
    return  QSqlDatabase::database(connectionname);
}

//插入用户数据
void Database_driver::insertUser(QSqlDatabase db, const QString count, const QString passward)
{
    QSqlQuery query(db);
    query.exec("CREATE TABLE IF NOT EXISTS user(id INTEGER PRIMARY KEY AUTOINCREMENT, count varchar(20) , passward varchar(20))");

    query.prepare("INSERT INTO user (count , passward) VALUES (:count , :passward)");
    query.bindValue(":count" , count);
    query.bindValue(":passward" , passward);
    query.exec();
}

//查找用户密码
QString Database_driver::findUser_Pssward(QSqlDatabase db, QString count)
{
    QSqlQuery query(db);
    query.prepare("SELECT count , passward from user where count=:count");
    query.bindValue(":count" , count);
    query.exec();
    if(false != query.next()){
        return query.value("passward").toString();
    }else{
        return NULL;
    }
}


